﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using StevensReceipts.Domain;
using StevensReceipts.Repository;

namespace StevensReceipts.Web.Areas.Service.Controllers
{
    public class ChartofAccountController : ApiControllerBase
    {
        public ChartofAccountController(IReceiptsDataSource db)
        {
            DataSource = db;
        }
        // GET api/chartofaccount/AllByBusiness/1
        [ActionName("AllByBusiness"),HttpGet]
        public IEnumerable<ChartofAccount> AllByBusinessId(int id)
        {
            IQueryable<ChartofAccount> accounts = DataSource.ChartofAccounts.Get(c => c.Business.Id == id);
            return accounts.Select(chartofAccount => chartofAccount);
        }

        // GET api/chartofaccount/AllByBusinessCategory?businessId=1&categoryId=1
        [ActionName("AllByBusinessCategory"),HttpGet]
        public IEnumerable<ChartofAccount> GetAllByBusinessGroupByCategory(int businessId, int categoryId)
        {
            ICollection<ChartofAccountAccountType> types = DataSource.CoaCategories.GetSingle(c=>c.Id == categoryId).Types;

            IQueryable<ChartofAccount> accounts = DataSource.ChartofAccounts.Get(c => c.Business.Id == businessId && types.Contains(c.AccountType));
            return accounts.Select(chartofAccount => chartofAccount);
        }

        // GET api/chartofaccount/5
        public ChartofAccount Get(int id)
        {
            var account = DataSource.ChartofAccounts.GetSingle(c=>c.Id == id);
            if(account == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);
            return account;
        }

        // POST api/chartofaccount
        public void Post(ChartofAccount account)
        {
            DataSource.ChartofAccounts.Add(account);
            DataSource.Save();
        }

        // PUT api/chartofaccount/5
        public void Put(int id, ChartofAccount account)
        {
            ChartofAccount coa = DataSource.ChartofAccounts.GetSingle(c=>c.Id == id);
            if (coa == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            coa.AccountType = account.AccountType;
            coa.Balance = account.Balance;
            coa.Business = account.Business;
            coa.Code = account.Code;
            coa.Description = account.Description;
            coa.Name = account.Name;
            coa.TaxRate = account.TaxRate;
            coa.TaxReturnPercent = account.TaxReturnPercent;
            coa.Modified = DateTime.Now;
            DataSource.Save();
        }

        // DELETE api/chartofaccount/5
        public void Delete(int id)
        {
            ChartofAccount coa = DataSource.ChartofAccounts.GetSingle(c => c.Id == id);
            if(coa != null)
            {
                DataSource.ChartofAccounts.Delete(coa);
                DataSource.Save();
            }
            else
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
    }
}
